//
//  LGSInputMappingDataSource.m
//  LogicSim
//
//  Created by Jeremy Pereira on 13/03/2013.
//
//

#import "LGSInputMappingDataSource.h"
#import "LGSModule.h"
#import "LGSName.h"
#import "LGSSplitter.h"

@implementation LGSInputMappingDataSource

@synthesize module = module_;

-(void) setModule: (LGSModule*) module
{
    module_ = module;
    [self reloadData];
}

-(void) reloadData
{
    [[self outlineView] reloadData];    
}


-(id) outlineView: (NSOutlineView*) outlineView
            child: (NSInteger) index
           ofItem: (id) item
{
    id ret = @"#undefined";
    if (item == nil)
    {
        ret = [[[self module] inputNames] objectAtIndex: index];
    }
    else if ([item isKindOfClass: [NSString class]])
    {
        NSArray* inputMappings = [[self module] inputMappingsFor: item];
        ret = [inputMappings objectAtIndex: index];
    }
    return ret;
}

-(BOOL) outlineView: (NSOutlineView*) outlineView isItemExpandable: (id) item
{
    return [self outlineView: outlineView numberOfChildrenOfItem: item] > 0;
}

-(NSInteger) outlineView: (NSOutlineView*) outlineView
  numberOfChildrenOfItem: (id) item
{
    NSInteger ret = 0;
    if (item == nil)
    {
        ret = [[[self module] inputNames] count];
    }
    else if ([item isKindOfClass: [NSString class]])
    {
        NSArray* inputMappings = [[self module] inputMappingsFor: item];
        ret = [inputMappings count];
    }
    return ret;
}

-(id)         outlineView: (NSOutlineView*) outlineView
objectValueForTableColumn: (NSTableColumn*) tableColumn
                   byItem: (id) item
{
    id ret = @"";
    if ([[tableColumn identifier] isEqualToString: @"input"])
    {
        if ([item isKindOfClass: [NSString class]])
        {
            ret = item;
        }
    }
    else if ([item isKindOfClass: [LGSName class]])
    {
        ret = [item valueForKey: [tableColumn identifier]];
    }
    return ret;
}

//-(void) outlineView: (NSOutlineView*) outlineView
//     setObjectValue: (id) object
//     forTableColumn: (NSTableColumn*) tableColumn
//             byItem: (id) item
//{
//}


@end
